#include "Knight.h"
#include "../Game/Engine.h"
#include "../Board/Chessboard.h"
#include "../Board/Square.h"

using namespace std;

namespace Core
{

	Knight::Knight(Player * player, Square * square)
		:Piece(player, square)
	{
	}


	Knight::~Knight(void)
	{
	}

	string Knight::name() const { return "Knight"; }
	ChType Knight::type() const { return ChType::ChKnight; }

	//Move * Knight::canMove(Square * dst)
	//{
	//	Move * mv = Move::createMove(this, dst);


	//	return mv;
	//}


	void Knight::updateValidMoves()
	{
		int r = this->square()->row();
		int c = this->square()->column();
		
		considerForValidMove(r+2, c+1);
		considerForValidMove(r+2, c-1);
		considerForValidMove(r-2, c+1);
		considerForValidMove(r-2, c-1);
		considerForValidMove(r+1, c+2);
		considerForValidMove(r+1, c-2);
		considerForValidMove(r-1, c+2);
		considerForValidMove(r-1, c-2);
	}

	void Knight::considerForValidMove(int r2, int c2)
	{
		const Chessboard * brd = Engine::instance()->chessboard();
		try
		{
			Move * mv = new Move(this, brd->at(r2, c2));
			if(checkDestination(mv))
				_validMoves[mv->to()->key()] = mv;
		}
		catch(...) { /* bypass this move */ }
	}


}
